package cn.tedu.nybikeT.controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import cn.tedu.nybikeT.pojo.JsonResult;
import cn.tedu.nybikeT.pojo.OperationDO;
import cn.tedu.nybikeT.service.IOperationService;
import cn.tedu.nybikeT.util.IPUtils;

@RestController // 表示本类中所有方法返回的都是json字符串
@RequestMapping("/operation")
public class OperationController {

	@Autowired
	IOperationService service;

	@RequestMapping("/save")
	public JsonResult<Void> saveOperation(OperationDO opt) {
		// 获取用户的ip
		String userIp = IPUtils.getIpAddr();
		// 将ip添加到opt对象中
		opt.setUserIp(userIp);
		// 调用业务层方法，添加一项操作记录
		service.saveOpeartion(opt);
		// 返回成功提示
		return new JsonResult<Void>(2000, "OK", null);
	}

	@RequestMapping("/subscribe")
	public SseEmitter subscribe(String id, String date, Integer stationCountLimit, Integer ipLimit, Long interval) {
		// 参数的非空验证 TODO

		// 创建SseEmitter对象
		SseEmitter sseEmitter = new SseEmitter(1 * 24 * 60 * 60 * 1000L);
		// 启动子线程
		MyRunner runner = new MyRunner(sseEmitter, interval, date, stationCountLimit, ipLimit);
		Thread t1 = new Thread(runner);
		t1.start();
		// 为sseEmitter设置onCompleation事件
		sseEmitter.onCompletion(new Runnable() {
			@Override
			public void run() {
				System.err.println(id + "=>SSE终止");
				// 结束子线程的发送数据的循环
				runner.stop();
			}
		});

		// 返回sseEmitter
		return sseEmitter;
	}

	class MyRunner implements Runnable {

		private SseEmitter sseEmitter;
		private String date;
		private Integer stationCountLimit;
		private Integer ipLimit;
		private long interval; // 推送的间隔
		private volatile boolean flag = true;// 循环的开关

		public MyRunner(SseEmitter sseEmitter, long interval, String date, Integer stationCountLimit, Integer ipLimit) {
			this.sseEmitter = sseEmitter;
			this.interval = interval;
			this.date = date;
			this.stationCountLimit = stationCountLimit;
			this.ipLimit = ipLimit;
		}

		public void stop() {
			this.flag = false;
		}

		@Override
		public void run() {
			// 周期性获取大屏所需数据，调用sseEmitter推送数据
			while (flag) {
				try {
					Thread.sleep(interval);
					sseEmitter.send(service.findOperationData(date, stationCountLimit, ipLimit));
				} catch (IOException e) {
					e.printStackTrace();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
